package com.itheima.controller;

import com.itheima.pojo.Actor;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SpringBootTest
public class test {
    private List<String> list = new ArrayList<>();
    private List<String> list1 = new ArrayList<>();
    private List<Integer> list2 = new ArrayList<>();
    private Map<String, Integer> map = new HashMap<>();


    @Test
    @DisplayName("过滤测试->stream流")
    public void test1() {
        Collections.addAll(list, "张三峰-男-21", "张无忌-男-35", "无底洞-女-18", "我是谁-女-14", "李大力-男-31");
        list.stream()
                .filter(s -> s.startsWith("张"))
                .forEach(s -> System.out.println(s));
        map.put("aaa", 1);
        map.put("bbbb", 1);
        map.put("dsa", 3);
        map.put("acv", 10);
        //map.keySet().stream().forEach(s -> System.out.println(s));
        //map.entrySet().stream().forEach(s-> System.out.println(s));
        //拿到值，存入新集合
        List<Integer> collect = map.entrySet()
                .stream()
                .map(s -> s.getValue())
                .collect(Collectors.toList());
        System.out.println(collect);

        list.stream()
                .filter(s -> "男".equals(s.split("-")[1]))
                .forEach(System.out::println);

    }

    @Test
    @DisplayName("只获取前三个元素")
    public void test2() {
        Collections.addAll(list, "张三峰", "张无忌", "无底洞", "我是谁", "李大力");
        list.stream()
                .limit(3)
                .forEach(System.out::println);
    }

    @Test
    @DisplayName("跳过元素输出")
    public void test3() {
        Collections.addAll(list, "张三峰", "张无忌", "无底洞", "我是谁", "李大力");
        list.stream()
                .skip(3)
                .forEach(s -> System.out.println(s));
    }

    @Test
    @DisplayName("元素去重")
    public void test4() {
        Collections.addAll(list, "张三峰", "张三峰", "张三疯", "张无忌", "无底洞", "我是谁", "李大力");
        list.stream()
                .distinct()
                .forEach(System.out::println);
    }

    @Test
    @DisplayName("合并元素")
    public void test5() {
        Collections.addAll(list, "张三峰", "张三峰", "张三疯", "张无忌", "无底洞", "我是谁", "李大力");
        Collections.addAll(list1, "AAA", "BBB");
        Stream.concat(list.stream(), list1.stream())
                .forEach(System.out::println);

    }

    @Test
    @DisplayName("stream流的类型转换测试")
    public void test6() {
        Collections.addAll(list, "张三疯-16", "熊大大-32", "熊大-99");
        List<Integer> collect = list.stream()
                .map(s -> Integer.parseInt(s.split("-")[1]))
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    @DisplayName("收集方法测试")
    public void test7() {
        Collections.addAll(list, "张三峰-男-21", "张无忌-男-35", "无底洞-女-18", "我是谁-女-14", "李大力-男-31");
     /*   List<String> tmpList = list.stream()
                .filter(s -> s.split("-")[1].equals("男"))
                .collect(Collectors.toList());
        System.out.print(tmpList);*/

        Map<String, Integer> map2 = list.stream().
                filter(s -> s.split("-")[1].equals("男"))
                .collect(Collectors.toMap(
                        s -> s.split("-")[0],
                        s -> Integer.parseInt(s.split("-")[2])
                ));
        System.out.println(map2);
    }

    @Test
    @DisplayName("数据过滤")
    public void test8() {
        Collections.addAll(list2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<Integer> collect = list2.stream()
                .filter(s -> s % 2 == 0)
                .collect(Collectors.toList());
        System.out.println(collect);

/*        Collections.addAll(list, "zhangsan,16", "lisi,32", "wangwu,99");
        Map<String, Integer> collect1 = list.stream()
                .filter(s -> Integer.parseInt(s.split(",")[1]) >= 24)
                .collect(Collectors.toMap(
                        s -> s.split(",")[0],
                        s -> Integer.parseInt(s.split(",")[1])
                ));

        System.out.println(collect1);*/

        Collections.addAll(list, "张三峰,23", "熊大,51", "张无忌,32", "无底洞,50", "我是谁,14");
        Collections.addAll(list1,"杨红,17","小白,23","杨花,17","小美,24","帕瓦,24");
        List<String> collect1 = list.stream()
                .filter(s -> s.split(",")[0].length() == 3)
                .limit(2)
                .collect(Collectors.toList());

        List<String> collect2 = list1.stream()
                .filter(s -> s.startsWith("杨"))
                .skip(1)
                .collect(Collectors.toList());
        List<Actor> collect3 = Stream.concat(collect1.stream(), collect2.stream())
                .map(s -> new Actor(s.split(",")[0], Integer.parseInt(s.split(",")[1])))
                .collect(Collectors.toList());
        System.out.println(collect3);


    }
}
